package com.koron.ticket.work.logicticket.electricalsecond.controller;

import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.common.core.business.print.ReportTask;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.ticket.utils.PrintUtils;
import com.koron.ticket.work.common.bean.FifteenTables;
import com.koron.ticket.work.common.bean.Print;
import com.koron.ticket.work.enums.PrintTemplate;
import com.koron.ticket.work.logicticket.electricalsecond.service.ElectricalSecondService;
import com.koron.ticket.work.workPermit.bean.WorkPermit;
import com.koron.ticket.work.workend.bean.WorkEnd;
import com.koron.ticket.work.workflow.bean.WorkFlow;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("work/electricalsecond")
@Api(tags = "两票管理-工作票-电气第二种工作票")
public class ElectricalSecondController {

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    private EamUser getCurrUser() {
        return ThreadLocalContext.get();
    }

    @Autowired
    private ElectricalSecondService electricalSecondService;

    /**
     * 填写阶段，保存功能
     * 1. 保存当前的数据（新增/更新）
     *
     * @param fifteenTables
     * @return Response对象
     * @version 1.0
     * @see Response
     * @see FifteenTables
     */
    @PostMapping("writeSave")
    @ApiOperation("填写-保存")
    public Response writeSave(@RequestBody FifteenTables fifteenTables) {
        return ADOConnection.runTask(getCurrDs(), electricalSecondService, "writeSave", Response.class, fifteenTables);
    }

    /**
     * 打印预览功能：
     * 1.所有状态的打印预览都是这个接口
     * 2.改接口返回的是一个url，用于拉起打印控件的打印预览
     *
     * @param id
     * @return Response对象
     * @version 1.0
     * @see Response
     */
    @GetMapping("printById")
    @ApiOperation("打印预览")
    public Response print(@RequestParam("id") String id) {
        List<Print> printList = ADOConnection.runTask(getCurrDs(), electricalSecondService, "getPrintData", ArrayList.class, id);
        String printTemplateId = PrintUtils.getDefaultPrintTemplateId(PrintTemplate.ElectricalSecond.getType());
        String url = ReportTask.printData(getCurrUser(), printTemplateId, printList);//存储打印数据，以及返回打印地址
        return Response.success("获取成功，拿返回地址回调打印", url);
    }

    /**
     * 填写阶段，提交功能
     * 1. 保存当前的数据（新增/更新）
     * 2. 初始化工作流
     * 3. 记录操作人
     *
     * @param fifteenTables
     * @return Response对象
     * @version 1.0
     * @see Response
     * @see FifteenTables
     */
    @PostMapping("writeSubmit")
    @ApiOperation("填写-提交")
    public Response writeSubmit(@RequestBody FifteenTables fifteenTables) {
        return ADOConnection.runTask(getCurrDs(), electricalSecondService, "writeSubmit", Response.class, fifteenTables);
    }

    /**
     * 待签发阶段，保存功能
     * 1. 仅保存签名的数据（新增/更新）
     *
     * @param workFlow:workTicketId,status,type,account,name, operationTime(电子签名的操作时间由电子签的回调接口来处理),deleteFlag（电子签的时候为1，普通签的时候为0）
     * @return Response对象
     * @version 1.0
     * @see Response
     * @see WorkFlow
     */
    @PostMapping("signSave")
    @ApiOperation("待签发-保存")
    public Response signSave(@RequestBody WorkFlow workFlow) {
        return ADOConnection.runTask(getCurrDs(), electricalSecondService, "signSave", Response.class, workFlow);
    }

    /**
     * 待会签阶段，保存功能
     * 1. 仅保存签名的数据（新增/更新）
     *
     * @param workFlow:workTicketId,status,type,account,name, operationTime(电子签名的操作时间由电子签的回调接口来处理),deleteFlag（电子签的时候为1，普通签的时候为0）
     * @return Response对象
     * @version 1.0
     * @see Response
     * @see WorkFlow
     */
    @PostMapping("countersignSave")
    @ApiOperation("待会签-保存")
    public Response countersignSave(@RequestBody WorkFlow workFlow) {
        return ADOConnection.runTask(getCurrDs(), electricalSecondService, "countersignSave", Response.class, workFlow);
    }

    /**
     * 待接收阶段，保存功能
     * 1. 仅保存签名的数据（新增/更新）
     *
     * @param workFlow
     * @return Response对象
     * @version 1.0
     * @see Response
     * @see WorkFlow
     */
    @PostMapping("acceptSave")
    @ApiOperation("待接收-保存")
    public Response acceptSave(@RequestBody WorkFlow workFlow) {
        return ADOConnection.runTask(getCurrDs(), electricalSecondService, "acceptSave", Response.class, workFlow);
    }

    /**
     * 待许可阶段，保存功能
     * 1. 保存待许可阶段的数据（新增/更新）
     * 2. 保存签名的数据（新增/更新）
     *
     * @param workPermit
     * @return Response对象
     * @version 1.0
     * @see Response
     * @see WorkPermit
     */
    @PostMapping("permitSave")
    @ApiOperation("待许可-保存")
    public Response permitSave(@RequestBody WorkPermit workPermit) {
        return ADOConnection.runTask(getCurrDs(), electricalSecondService, "permitSave", Response.class, workPermit);
    }

    /**
     * 工作待终结阶段，保存功能
     * 1. 保存工作待终结阶段的数据（新增/更新）
     * 2. 保存签名的数据（新增/更新）
     *
     * @param workEnd
     * @return Response对象
     * @version 1.0
     * @see Response
     * @see WorkEnd
     */
    @PostMapping("workEndSave")
    @ApiOperation("工作待终结-保存")
    public Response workEndSave(@RequestBody WorkEnd workEnd) {
        return ADOConnection.runTask(getCurrDs(), electricalSecondService, "workEndSave", Response.class, workEnd);
    }

    /**
     * 工作票待终结阶段，保存功能
     * 1. 保存工作票待终结阶段的数据（新增/更新）
     * 2. 保存签名的数据（新增/更新）
     *
     * @param workEnd
     * @return Response对象
     * @version 1.0
     * @see Response
     * @see WorkEnd
     */
    @PostMapping("ticketEndSave")
    @ApiOperation("工作票待终结-保存")
    public Response ticketEndSave(@RequestBody WorkEnd workEnd) {
        return ADOConnection.runTask(getCurrDs(), electricalSecondService, "ticketEndSave", Response.class, workEnd);
    }

    /**
     * 工作流回调接口
     * 1. 除了填写阶段，和已归档前一个阶段（不同的票不一样）都要走这个接口
     * 2. 处理驳回和通过的情况
     *
     * @param redirectQuery
     * @return Response对象
     * @version 1.0
     * @see Response
     */
    @PostMapping("/workflowUpdate")
    @ApiOperation(value = "工作流回调接口",hidden = true)
    public Response updateState(@RequestBody RedirectQuery redirectQuery) {
        return ADOConnection.runTask(getCurrDs(), electricalSecondService, "workflowUpdate", Response.class, redirectQuery);
    }

    /**
     * 工作流结束接口：
     * 1. 已归档签一个阶段的提交，会走这个接口处理
     *
     * @param redirectQuery
     * @return Response对象
     * @version 1.0
     * @see Response
     */
    @PostMapping("/workflowUpdateEnd")
    @ApiOperation(value = "结束时的修改方法, 非前端接口",hidden = true)
    public Response updateEnd(@RequestBody RedirectQuery redirectQuery) {
        return ADOConnection.runTask(getCurrDs(), electricalSecondService, "workflowUpdateEnd", Response.class, redirectQuery);
    }

    /**
     * 工作流作废：
     * 1. 工作流作废时的接口
     * 2. 作废仅变更工作票的状态；其他数据保留
     *
     * @param redirectQuery
     * @return Response对象
     * @version 1.0
     * @see Response
     */
    @PostMapping("/workflowUpdateCancel")
    @ApiOperation(value = "作废时的修改方法, 非前端接口",hidden = true)
    public Response updateCancel(@RequestBody RedirectQuery redirectQuery) {
        return ADOConnection.runTask(getCurrDs(), electricalSecondService, "workflowUpdateCancel", Response.class, redirectQuery);
    }

}
